<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;

class Book extends Model
{
    use SoftDeletes;

    protected $fillable = ['id', 'key', 'name', 'full_name', 'introduce', 'word_num', 'origin',
        'organization', 'publisher', 'cover', 'sort', 'is_enable', 'version'];

    public static array $category = [
//        0 => '幼儿',
        1 => '小学',
        2 => '初中',
        3 => '高中',
        4 => '四级',
        5 => '六级',
        6 => '八级',
//        7 => '考研',
    ];

    public function words(): HasMany
    {
        return $this->hasMany(Word::class);
    }

    /**
     * 收藏该Book的用户
     */
    public function users(): MorphToMany
    {
        return $this->morphToMany(User::class, 'followable');
    }

    public static function option(): Collection
    {
        return self::query()->scopes(['enable', 'sort'])->pluck('name', 'id');
    }
}
